package com.qigao.mall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.api.*;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.*;
import com.qigao.mall.commons.param.ChannelRespParam;
import com.qigao.mall.commons.param.CommonReqParam;
import com.qigao.mall.commons.utils.DateUtil;
import com.qigao.mall.jdbc.model.TbMerChannel;
import com.qigao.mall.jdbc.model.TbShopInfo;
import com.qigao.mall.service.channel.ChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luiz
 * @Title: com.qigao.mall.service.impl
 * @ProjectName three-mall
 * @date 2020-04-2020/4/22 14:11
 * @Description:
 */
@Service
public class OpenShopServiceImpl implements OpenShopService {
    @Resource
    private MerchantEnterService merchantEnterService;
    @Resource
    private ShopInfoService shopInfoService;
    @Resource
    private OrderService orderService;
    @Resource
    private ItemService itemService;
    @Resource
    private ShopEmpService shopEmpService;
    @Resource
    private ChannelRouteService channelRouteService ;
    @Resource
    private ChannelService iChannelService;
    @Resource
    private ItemCatService itemCatService;

    @Override
    public Result openShop(OpenShopDto openShopDto) {
        //1.检查商户信息 todo 商户不能进件,暂时取平台商户号
        //MerInfoDto merInfoDto=merchantEnterService.checkMerInfo(openShopDto);
        //2.检查店铺信息 付款成功之后再创建店铺
        ShopInfoCreateDto shopInfoCreateDto=new ShopInfoCreateDto();
        BeanUtils.copyProperties(openShopDto,shopInfoCreateDto);
        shopInfoCreateDto.setMerNo(openShopDto.getSysMerNo());
        TbShopInfo tbShopInfo=shopInfoService.create(shopInfoCreateDto);
        //3.创建订单
        OrderCreateDto orderCreateDto= buildOrder(openShopDto);
        Result orderResult=orderService.createOrder(orderCreateDto);
        shopInfoCreateDto.setShopNo(tbShopInfo.getShopNo());
        shopInfoCreateDto.setOrderNo((String)orderResult.getData());
        shopInfoService.updateByShopNo(shopInfoCreateDto);
        return orderResult;
    }

    /**
     * 默认开通店铺、有效期15天
     * @param openShopDto
     * @return
     */
    @Override
    public Result autoOpenShop(OpenShopDto openShopDto) {
        //1.创建店铺
        ShopInfoCreateDto shopInfoCreateDto=new ShopInfoCreateDto();
        BeanUtils.copyProperties(openShopDto,shopInfoCreateDto);
        shopInfoCreateDto.setMerNo(openShopDto.getMerNo());
        shopInfoCreateDto.setValidStatus(StatusEnum.VALID.getCode());
        shopInfoCreateDto.setValidBeginTime(new Date());
        shopInfoCreateDto.setValidEndTime(DateUtil.addMonth(new Date(),15));
        TbShopInfo tbShopInfo=shopInfoService.create(shopInfoCreateDto);
        //2.店铺关联员工
        ShopEmpCreateDto shopEmpCreateDto=new ShopEmpCreateDto();
        shopEmpCreateDto.setShopNo(tbShopInfo.getShopNo());
        shopEmpCreateDto.setShopName(tbShopInfo.getShopName());
        shopEmpCreateDto.setUserNo(tbShopInfo.getCreateNo());
        shopEmpCreateDto.setValidStatus(StatusEnum.VALID.getCode());
        shopEmpService.create(shopEmpCreateDto);
        //3.微店铺创建默认分类
        itemCatService.createDefult(tbShopInfo);
        //4.生成店铺二维码
        return getWeiChatQrCode(tbShopInfo.getShopNo(),openShopDto.getAccessToken());
    }

    @Override
    public void payNotify(String orderNo) {
        Result orderResult=orderService.findOrderByNo(orderNo);
        OrderResultDto orderResultDto=(OrderResultDto)orderResult.getData();
        if(OrderTypeEnum.OPEN_SHOP.getCode().equals(orderResultDto.getOrderType())){
            String itemNo=orderResultDto.getOrderDetailResultDtoList().get(0).getItemNo();
            SaleProductEnum saleProductEnum=SaleProductEnum.parse(itemNo);
            ShopInfoDto shopInfoDto=shopInfoService.findByOrderNo(orderResultDto.getOrderNo());
            if(StatusEnum.VALID.getCode()!=shopInfoDto.getValidStatus()){
                ShopInfoCreateDto shopInfoCreateDto=new ShopInfoCreateDto();
                shopInfoCreateDto.setId(shopInfoDto.getId());
                shopInfoCreateDto.setOrderNo(orderResultDto.getOrderNo());
                shopInfoCreateDto.setValidStatus(StatusEnum.VALID.getCode());
                shopInfoCreateDto.setValidBeginTime(new Date());
                shopInfoCreateDto.setValidEndTime(DateUtil.addMonth(new Date(),saleProductEnum.getMonthNum()));
                shopInfoService.update(shopInfoCreateDto);
                ShopEmpCreateDto shopEmpCreateDto=new ShopEmpCreateDto();
                shopEmpCreateDto.setShopNo(shopInfoDto.getShopNo());
                shopEmpCreateDto.setShopName(shopInfoDto.getShopName());
                shopEmpCreateDto.setUserNo(shopInfoDto.getCreateNo());
                shopEmpCreateDto.setValidStatus(StatusEnum.VALID.getCode());
                shopEmpService.create(shopEmpCreateDto);
            }
        }
    }

    @Override
    public Result getWeiChatQrCode( String shopNo,String accessToken) {
        ShopInfoDto shopInfoDto=shopInfoService.findByShopNo(shopNo);
        ShopQrCodeResultDto shopQrCodeResultDto=new ShopQrCodeResultDto();
        BeanUtils.copyProperties(shopInfoDto,shopQrCodeResultDto);
        if(shopInfoDto!=null&& StringUtils.isNotEmpty(shopInfoDto.getShopQrcode())){
            return Result.newSuccess(shopQrCodeResultDto);
        }
        TbMerChannel tbMerChannel=channelRouteService.findChannelRouteShop(shopNo, WebSourceEnum.WEICHAT_JSAPI.getCode(),PayWayEnum.WEICHAT.getPayWayCode());
        if(tbMerChannel==null){
            return Result.newError(ResultCode.CHANNEL_CONFIG_NOT_EXITS);
        }
        MerChannelDto merChannelDto=new MerChannelDto();
        merChannelDto.setMerNo(tbMerChannel.getChannelPartenerNo());
        merChannelDto.setMerName(tbMerChannel.getMerName());
        merChannelDto.setAppId(tbMerChannel.getAppId());
        merChannelDto.setAppSecret(tbMerChannel.getAppSecret());
        merChannelDto.setChannelAction(ChannelActionEnum.CREATE_QRCODE.getCode());
        merChannelDto.setProductNo(tbMerChannel.getProductNo());
        merChannelDto.setChannelCode(tbMerChannel.getChannelCode());
        CommonReqParam commonReqParam=new CommonReqParam();
        JSONObject reqContent=new JSONObject();
        reqContent.put("shopNo",shopNo);
        reqContent.put("accessToken",accessToken);
        commonReqParam.setReqContent(reqContent.toJSONString());
        ChannelRespParam channelRespParam=iChannelService.channelProcess(merChannelDto,commonReqParam);
        if(ChannelCodeEnum.SUCCESS.getCode().equals(channelRespParam.getRespCode())){
            String imageCode=channelRespParam.getRespContent();
            ShopInfoCreateDto shopInfoCreateDto=new ShopInfoCreateDto();
            shopInfoCreateDto.setShopNo(shopNo);
            shopInfoCreateDto.setShopQrcode(imageCode);
            shopInfoService.updateByShopNo(shopInfoCreateDto);
            shopQrCodeResultDto.setShopQrcode(imageCode);
            return Result.newSuccess(shopQrCodeResultDto);
        }
        return Result.newError(ResultCode.SHOP_QRCODE_FAIL);
    }



    private OrderCreateDto buildOrder(OpenShopDto openShopDto) {
        OrderCreateDto orderCreateDto=new OrderCreateDto();
        BeanUtils.copyProperties(openShopDto,orderCreateDto);
        orderCreateDto.setMerNo(openShopDto.getSysMerNo());
        orderCreateDto.setShopNo(openShopDto.getSysShopNo());
        orderCreateDto.setBuylerId(openShopDto.getOperNo());
        orderCreateDto.setOrderType(OrderTypeEnum.OPEN_SHOP.getCode());
        List orderDetailDtos=new ArrayList();
        OrderCreateDetailDto orderCreateDetailDto=new OrderCreateDetailDto();
        orderCreateDetailDto.setItemNo(SaleProductEnum.COMMON_SHOP.getSaleProductCode());
        orderCreateDetailDto.setStockNo(SaleProductEnum.COMMON_SHOP.getSaleProductCode());
        orderCreateDetailDto.setBuyNum("1");
        orderDetailDtos.add(orderCreateDetailDto);
        orderCreateDto.setOrderCreateDetailDtoList(orderDetailDtos);
        itemService.saveSaleProduct(openShopDto,SaleProductEnum.COMMON_SHOP);
        return orderCreateDto;
    }
}
